﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class SearchFlight4 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["DepartFlights"] == null || Session["Adult"] == null || Session["Child"] == null || Session["Senior"] == null)
        {
            Response.Redirect("~/SearchFlight3.aspx");
        }

        if (!Page.IsPostBack)
        {
            Session["DepartTicketList"] = null;
            Session["DepartRequestType"] = null;
            Session["ReturnTicketList"] = null;
            Session["ReturnRequestType"] = null;
        }

        int numofPass = Convert.ToInt32(Session["Adult"]) + Convert.ToInt32(Session["Child"]) + Convert.ToInt32(Session["Senior"]);

        #region Depart Flight
        BigFlight DepartFlights = Session["DepartFlights"] as BigFlight;

        if (!Page.IsPostBack)
        {
            #region From to numOfStop
            lblDepartFrom.Text = DepartFlights.Origin.FullName;
            lblDepartTo.Text = DepartFlights.Destination.FullName;
            lblDepartStop.Text = (DepartFlights.FlightList.Count - 1).ToString() + "stop(s)";
            #endregion

            int BussPrice = 0;
            #region Buss Price
            foreach (Flight flight in DepartFlights.FlightList)
            {
                BussPrice += flight.BussPrice;
                rbDepartBuss.Text = "<br />" + BussPrice.ToString() + "USD";
                if (flight.AvailBussSeat < numofPass)
                {
                    rbDepartBuss.Text = "<br />Not Enough Seats";
                    rbDepartBuss.Enabled = false;
                    break;
                }
            }
            #endregion

            int FirstPrice = 0;
            #region First Price
            foreach (Flight flight in DepartFlights.FlightList)
            {
                FirstPrice += flight.FirstPrice;
                rbDepartFirst.Text = "<br />" + FirstPrice.ToString() + "USD";
                if (flight.AvailFirstSeat < numofPass)
                {
                    rbDepartFirst.Text = "<br />Not Enough Seats";
                    rbDepartFirst.Enabled = false;
                    break;
                }
            }
            #endregion

            int ClubPrice = 0;
            #region Club Price
            foreach (Flight flight in DepartFlights.FlightList)
            {
                ClubPrice += flight.ClubPrice;
                rbDepartClub.Text = "<br />" + ClubPrice.ToString() + "USD";
                if (flight.AvailClubSeat < numofPass)
                {
                    rbDepartClub.Text = "<br />Not Enough Seats";
                    rbDepartClub.Enabled = false;
                    break;
                }
            }
            #endregion

            #region Buy or Book
            if (DateTime.Now.AddDays(15).Date > DepartFlights.DepartTime.Date)
            {
                DropDownList1.Enabled = false;
                lblNoBook1.Visible = true;
            }
            #endregion
        }
        #region Derpart Detail

        dlDepartDetail.DataSource = DepartFlights.FlightList;
        dlDepartDetail.DataBind();
        #endregion
        #endregion

        if (Session["ReturnFlights"] != null)
        {
            PlaceHolder1.Visible = true;
            #region Return Flight
            BigFlight ReturnFlights = Session["ReturnFlights"] as BigFlight;

            if (!Page.IsPostBack)
            {
                #region From to numOfStop
                lblReturnFrom.Text = ReturnFlights.Origin.FullName;
                lblReturnTo.Text = ReturnFlights.Destination.FullName;
                lblReturnStop.Text = (ReturnFlights.FlightList.Count - 1).ToString() + "stop(s)";
                #endregion

                int BussPrice = 0;
                #region Buss Price
                foreach (Flight flight in ReturnFlights.FlightList)
                {
                    BussPrice += flight.BussPrice;
                    rbReturnBuss.Text = "<br />" + BussPrice.ToString();
                    if (flight.AvailBussSeat < numofPass)
                    {
                        rbReturnBuss.Text = "<br />Not Enough Seats";
                        rbReturnBuss.Enabled = false;
                        break;
                    }
                }
                #endregion

                int FirstPrice = 0;
                #region First Price
                foreach (Flight flight in ReturnFlights.FlightList)
                {
                    FirstPrice += flight.FirstPrice;
                    rbReturnFirst.Text = "<br />" + FirstPrice.ToString();
                    if (flight.AvailFirstSeat < numofPass)
                    {
                        rbReturnFirst.Text = "<br />Not Enough Seats";
                        rbReturnFirst.Enabled = false;
                        break;
                    }
                }
                #endregion

                int ClubPrice = 0;
                #region Club Price
                foreach (Flight flight in ReturnFlights.FlightList)
                {
                    ClubPrice += flight.ClubPrice;
                    rbReturnClub.Text = "<br />" + ClubPrice.ToString();
                    if (flight.AvailClubSeat < numofPass)
                    {
                        rbReturnClub.Text = "<br />Not Enough Seats";
                        rbReturnClub.Enabled = false;
                        break;
                    }
                }
                #endregion

                #region Buy or Book
                if (DateTime.Now.AddDays(14).Date > ReturnFlights.DepartTime.Date)
                {
                    DropDownList2.Enabled = false;
                    lblNoBook2.Visible = true;
                }
                #endregion
            }
            #region Derpart Detail

            dlReturnDetail.DataSource = ReturnFlights.FlightList;
            dlReturnDetail.DataBind();
            #endregion
            #endregion

        }
    }

    protected void LinkButton3_Command(object sender, CommandEventArgs e)
    {
        if (e.CommandName == "View Detail")
        {
            LinkButton3.CommandName = "Close";
            LinkButton3.Text = "Close";
            phDepartDetail.Visible = true;
        }
        else if (e.CommandName == "Close")
        {
            LinkButton3.CommandName = "View Detail";
            LinkButton3.Text = "View Detail";
            phDepartDetail.Visible = false;
        }
    }
    protected void LinkButton1_Command(object sender, CommandEventArgs e)
    {
        if (e.CommandName == "View Detail")
        {
            LinkButton1.CommandName = "Close";
            LinkButton1.Text = "Close";
            phReturnDetail.Visible = true;
        }
        else if (e.CommandName == "Close")
        {
            LinkButton1.CommandName = "View Detail";
            LinkButton1.Text = "View Detail";
            phReturnDetail.Visible = false;
        }
    }

    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if ((!rbDepartBuss.Checked) && (!rbDepartFirst.Checked) && (!rbDepartClub.Checked))
        {
            CustomValidator1.Text = "Please choose your Seat at Depart flight";
            args.IsValid = false;
        }
        else if ((Session["ReturnFlights"] != null) && (!rbReturnBuss.Checked) && (!rbReturnFirst.Checked) && (!rbReturnClub.Checked))
        {
            CustomValidator1.Text = "Please choose your Seat at Return flight";
            args.IsValid = false;
        }
        else
        {
            args.IsValid = true;
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            List<Ticket> DepartTicketList = new List<Ticket>();
            BigFlight DepartFlights = Session["DepartFlights"] as BigFlight;
            int numofPass = Convert.ToInt32(Session["Adult"]) + Convert.ToInt32(Session["Child"]) + Convert.ToInt32(Session["Senior"]);

            #region Depart Ticket List
            foreach (Flight flight in DepartFlights.FlightList)
            {
                Ticket ticket = new Ticket();
                ticket.Flight = flight;
                TicketDetail[] ticketdetail = new TicketDetail[numofPass];
                for (int i = 0; i < numofPass; i++)
                {
                    ticketdetail[i] = new TicketDetail();
                }
                foreach (TicketDetail _detail in ticketdetail)
                {
                    if (rbDepartBuss.Checked)
                    {
                        _detail.SeatClass = "Bussiness";
                    }
                    else if (rbDepartFirst.Checked)
                    {
                        _detail.SeatClass = "First";
                    }
                    else
                    {
                        _detail.SeatClass = "Club";
                    }
                    ticket.TicketDetail.Add(_detail);
                }

                DepartTicketList.Add(ticket);
            }
            #endregion
            Session["DepartTicketList"] = DepartTicketList;
            Session["DepartRequestType"] = DropDownList1.SelectedValue;

            if ((Session["ReturnFlights"] != null))
            {
                List<Ticket> ReturnTicketList = new List<Ticket>();
                BigFlight ReturnFlights = Session["ReturnFlights"] as BigFlight;

                #region Return Ticket List
                foreach (Flight flight in ReturnFlights.FlightList)
                {
                    Ticket ticket = new Ticket();
                    ticket.Flight = flight;
                    TicketDetail[] ticketdetail = new TicketDetail[numofPass];
                    for (int i = 0; i < numofPass; i++)
                    {
                        ticketdetail[i] = new TicketDetail();
                    }
                    foreach (TicketDetail _detail in ticketdetail)
                    {
                        if (rbReturnBuss.Checked)
                        {
                            _detail.SeatClass = "Bussiness";
                        }
                        else if (rbReturnFirst.Checked)
                        {
                            _detail.SeatClass = "First";
                        }
                        else
                        {
                            _detail.SeatClass = "Club";
                        }
                        ticket.TicketDetail.Add(_detail);
                    }

                    ReturnTicketList.Add(ticket);
                }
                #endregion

                Session["ReturnTicketList"] = ReturnTicketList;
                Session["ReturnRequestType"] = DropDownList2.SelectedValue;
            }
            else
            {
                Session["ReturnTicketList"] = null;
                Session["ReturnRequestType"] = null;
            }

            Response.Redirect("~/Users/PassengerDetail.aspx");
        }
    }
}
